//
//  WebViewController.m
//  Nooryes
//
//  Created by uuu9 on 15/10/29.
//  Copyright © 2015年 Jason. All rights reserved.
//

#import "WebViewController.h"
@interface WebViewController ()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;

@end

@implementation WebViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _webView.navigationDelegate = self;
    _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _webView.allowsBackForwardNavigationGestures = YES;
    _webView.configuration.allowsInlineMediaPlayback = YES;
    _webView.configuration.allowsPictureInPictureMediaPlayback = YES;
    _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_webView];
    
    
//    NSString *urlString = self.urlString;
//    NSURL *url = [NSURL URLWithString:urlString];
//    NSData *data = [NSData dataWithContentsOfURL:url];
//    [_webView loadData:data MIMEType:@"text/html" textEncodingName:@"GBK" baseURL:[NSURL URLWithString:self.urlString]];
    
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
    
//    _progressProxy = [[NJKWebViewProgress alloc] init];
//    self.webView.delegate = _progressProxy;
//    _progressProxy.webViewProxyDelegate = self;
//    _progressProxy.progressDelegate = self;
//    
//    CGFloat progressBarHeight = 2.0f;
//    CGRect navigaitonBarBounds = self.navigationController.navigationBar.bounds;
//    CGRect barFrame = CGRectMake(0, navigaitonBarBounds.size.height - progressBarHeight, navigaitonBarBounds.size.width, progressBarHeight);
//    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
//    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
}


- (void)backButtonTapped{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}



//webview加载urlrequest后，这个方法决定是同意加载或者取消
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
   
        decisionHandler(WKNavigationActionPolicyAllow);
}

//导航开始(页面加载开始？)
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    
}

//webview接收到服务器respones之后，这个方法决定同意或者取消加载
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
   
    decisionHandler(WKNavigationResponsePolicyAllow);
    
}

//当内容返回时调用 content starts arriving for the main frame
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    
}



- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
    
//
//    NSString *title=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
//    self.navigationItem.title = title;
}

//接收到服务器重定向请求的时候
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
{
}






- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    // js 里面的alert实现，如果不实现，网页的alert函数无效
 
    
}

-(void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
    
    
    
    
}

@end
